package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.ProductionWareHouseDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.ProductionWareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductionWareHouseServiceImpl implements ProductionWareHouseService {
    @Autowired(required = false)
    ProductionWareHouseDao productionWareHouseDao;

    @Override
    public ResponseEntity deleteOne(String shipment_No) {
       int i=productionWareHouseDao.deleteOne(shipment_No);
       if(i>0){
           return ResponseEntity.SUCCESS;
       }else {
           return ResponseEntity.Fail;
       }

    }

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<FGI> list =  productionWareHouseDao.findAll();
        PageInfo<FGI> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    @Override
    public ResponseEntity finddeliveryrecordEdit() {
        return new ResponseEntity<>(200,"成功",productionWareHouseDao.finddeliveryrecordEdit());
    }

    @Override
    public ResponseEntity findWarehousingRecord() {
        return new ResponseEntity<>(200,"成功",productionWareHouseDao.findWarehousingRecord());
    }

    @Override
    public ResponseEntity findinventorydetails() {
       return new ResponseEntity(200,"查寻成功", productionWareHouseDao.findinventorydetails());

    }

    @Override
    public ResponseEntity search(String material_code) {
        return new  ResponseEntity(200,"查寻成功", productionWareHouseDao.search(material_code));
    }

    @Override
    public ResponseEntity addOne(Deliveryrecord deliveryrecord) {
        int i=productionWareHouseDao.addOne(deliveryrecord);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else{
            return ResponseEntity.Fail;
        }

    }

    @Override
    public ResponseEntity findAlldelivery_record(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Delivery_record> list =  productionWareHouseDao.findAlldelivery_record();
        PageInfo<Delivery_record> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    @Override
    public ResponseEntity findAlldelivery_record() {
        return new ResponseEntity<>(200,"成功",productionWareHouseDao.findAlldelivery_record());
    }

    @Override
    public ResponseEntity updateOne(Deliveryrecord deliveryrecord) {
        int i=productionWareHouseDao.updateOne(deliveryrecord);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.Fail;
        }
    }

    @Override
    public ResponseEntity findShipment_No(String shipment_no) {
        return new  ResponseEntity(200,"查寻成功", productionWareHouseDao.findShipment_No(shipment_no));
    }


    @Override
    public ResponseEntity addStore(Warehousingrecord warehousingrecord) {
        int i=productionWareHouseDao.addStore(warehousingrecord);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.Fail;
        }
    }

    @Override
    public ResponseEntity updateStore(Warehousingrecord warehousingrecord) {
        int i=productionWareHouseDao.updateStore(warehousingrecord);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.Fail;
        }
    }

    @Override
    public ResponseEntity deleteStore(String receipt_no) {
        int i=productionWareHouseDao.deleteStore(receipt_no);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.Fail;
        }

    }

    @Override
    public ResponseEntity findReceipt_No(String receipt_no) {
        return new  ResponseEntity(200,"查寻成功", productionWareHouseDao.findReceipt_No(receipt_no));
    }

    @Override
    public ResponseEntity updatewarehouse_num(String material_code, int receipt_quantity) {
        FGI f=productionWareHouseDao.findPW(material_code);
       receipt_quantity=f.getWarehouse_num()+receipt_quantity;
       int i= productionWareHouseDao.updatewarehouse_num(material_code,receipt_quantity);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.Fail;
        }
    }

    @Override
    public ResponseEntity update(String material_code, int shipped_qty) {
       FGI f=productionWareHouseDao.findPW(material_code);
        shipped_qty=f.getWarehouse_num()-shipped_qty;
        int i=productionWareHouseDao.update(material_code,shipped_qty);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.Fail;
        }
    }

    @Override
    public ResponseEntity findinventorydetails(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Inventorydetails> list =  productionWareHouseDao.findinventorydetails();
        PageInfo<Inventorydetails> pageInfo = PageInfo.of(list);
        return new ResponseEntity<>(200,"成功",pageInfo);
    }

    @Override
    public ResponseEntity findOrder() {
       return new ResponseEntity<>(200,"成功",productionWareHouseDao.findOrder());
    }

    @Override
    public ResponseEntity findPW(String material_code) {
        return new ResponseEntity<>(200,"成功",productionWareHouseDao.findPW(material_code));
    }

    @Override
    public ResponseEntity updateOrderStatus(String order_num) {
      int i=productionWareHouseDao.updateOrderStatus(order_num);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.Fail;
        }
    }

    @Override
    public ResponseEntity importExcel(List<Warehousingrecord> list) {
        for (Warehousingrecord w: list){
            FGI f=productionWareHouseDao.findPW(w.getMaterial_code());
          int receipt_quantity=f.getWarehouse_num()+w.getReceipt_quantity();
          productionWareHouseDao.updatewarehouse_num(w.getMaterial_code(),receipt_quantity);
        }

        int i = productionWareHouseDao.importExcel(list);
        if (i>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

}
